package com.woniu.new_orange.staff_module_dong.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.new_orange.entity.Staff;
import com.woniu.new_orange.staff_module_dong.dto.StaffDto;
import com.woniu.new_orange.staff_module_dong.service.PermissionService;
import com.woniu.new_orange.staff_module_dong.service.StaffService;
import com.woniu.new_orange.staff_module_dong.vo.PermissionVo;
import com.woniu.new_orange.staff_module_dong.vo.StaffVo;
import com.woniu.new_orange.util.Result;
import com.woniu.new_orange.util.Verify;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Program: new_orange->StaffController
 * @Description: 员工controller
 * @Author: donghang
 * @Date: 2023-01-14 10:02
 * @Version： 1.0
 **/
@RestController
@RequestMapping("/staffs")
@Api(value = "员工操作",tags = "员工详细操作")
public class StaffController {

    @Autowired
    private Verify verify;

    @Autowired
    private StaffService staffService;

    @Autowired
    private PermissionService permissionService;

    //  董航  2023/2/9 19:37  微信扫码登录
    @PostMapping("/loginByWeChat")
    public Result sel(@RequestBody StaffDto staffDto){
        PageHelper.startPage(staffDto.getCurrentPage(),staffDto.getPageSize());
        List<StaffVo> staffVos = staffService.selStaffByLike(staffDto);
        PageInfo<StaffVo> staffVoPageInfo = new PageInfo<>(staffVos);
        return Result.ok().data("staffList",staffVos).data("total",staffVoPageInfo.getTotal());
    }

    //  董航  2023/2/7 11:38  员工模糊登录+动态菜单
    @PostMapping("/loginDo")
    @ApiOperation(value = "员工模糊登录+动态菜单",notes = "员工通过手机，邮箱，账号进行登录+动态菜单获取并返回给前端")
    @ApiResponses({@ApiResponse(code = 200,message = "成功"),
            @ApiResponse(code = 401,message = "未认证"),
            @ApiResponse(code = 403,message = "无权限"),
            @ApiResponse(code = 404,message = "未找到")})
    public Result toLogin(@RequestBody StaffDto staffDto){
        if (verify.isEmail(staffDto.getStaffAccount())) {
            Staff staff = staffService.loginByEmail(staffDto);
            if (staff != null) {
                List<PermissionVo> permissionVos = permissionService.selMenuByStaffId(staff.getStaffId(), 0);
                return Result.ok("登录成功！").data("menulist",permissionVos).data("staff",staff);
            } else {
                return Result.error("账户信息异常，请联系主管解决");
            }
        } else if (verify.isMobile(staffDto.getStaffAccount())){
            Staff staff = staffService.loginByPhone(staffDto);
            if (staff != null) {
                List<PermissionVo> permissionVos = permissionService.selMenuByStaffId(staff.getStaffId(), 0);
                return Result.ok("登录成功！").data("menulist",permissionVos).data("staff",staff);
            } else {
                return Result.error("账户信息异常，请联系主管解决");
            }
        } else {
            Staff staff = staffService.staffLogin(staffDto);
            if (staff != null) {
                List<PermissionVo> permissionVos = permissionService.selMenuByStaffId(staff.getStaffId(), 0);
                return Result.ok("登录成功！").data("menulist",permissionVos).data("staff",staff);
            } else {
                return Result.error("账户信息异常，请联系主管解决");
            }
        }
    }

    //  董航  2023/2/2 14:14  展示员工信息+模糊查询
    @PostMapping("/query")
    @ApiOperation(value = "展示员工信息+模糊查询",notes = "展示员工信息+模糊查询")
    @ApiResponses({@ApiResponse(code = 200,message = "成功"),
            @ApiResponse(code = 401,message = "未认证"),
            @ApiResponse(code = 403,message = "无权限"),
            @ApiResponse(code = 404,message = "未找到")})
    public Result selStaffByLike(@RequestBody StaffDto staffDto){
        PageHelper.startPage(staffDto.getCurrentPage(),staffDto.getPageSize());
        List<StaffVo> staffVos = staffService.selStaffByLike(staffDto);
        PageInfo<StaffVo> staffVoPageInfo = new PageInfo<>(staffVos);
        return Result.ok().data("staffList",staffVos).data("total",staffVoPageInfo.getTotal());
    }
}
